********************************************************************************
** 	TITLE: e16_predict_time_in_jail_het.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Figure XVI: Heterogeneity by Predicted Time-in-Jail Quartiles

********************************************************************************
********************************************************************************
set sortseed 13

		use "$output_data/prediction.dta",clear

******************************************
* Prepare Data
******************************************
			//stay ignite_0 for Stay measures
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
				
***** Predict time in jail using 2015 sample
		//Using design and auxiliary controls for predictions. 
		cap drop stay_hat 
		logit stay $design_control $auxiliary_control ///
			if stay>=0& booking_year<=2015, cluster(inmate)
		predict stay_hat, pr

		su stay_hat, de
	
******************************************
* Make Figure
******************************************

***** Heterogeneity by quartiles
		cap drop risk_tile*
		xtile risk_tile = stay_hat if booking_year>=2016 , n(4)


		forvalues t=2/4{
			gen risk_tile_`t' = risk_tile==`t'
			}

		foreach v in  num_maj_weekly return_3m {
				//stay ignite_0 for misconduct
			if "`v'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
				//stay ignite_0 for Recidivism
			if "`v'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}


***** interaction terms						
		cap drop *_grpvar*

		forvalues t=2/4{
			foreach c of varlist ignite_0 stay D_ignite_proxy interact_proxy_any D_remove_any {
										
										gen `c'_grpvar_`t' = `c' * risk_tile_`t'
										
										}
									}

***** IV Regression												
		ivreghdfe `v'  ( ignite_0_grpvar_* stay_grpvar_* ignite_0 stay = ///
						interact_proxy_any_grpvar_* D_remove_any_grpvar_* interact_proxy_any D_remove_any) ///
						D_ignite_proxy_grpvar_* D_ignite_proxy risk_tile_* $auxiliary_control ///
						if `v'>=0&booking_year>2015,  ///
						cluster(inmate) ///
						a( $design_control ) 

//IGNITE Effect by Quartiles						
			mat R=J(10,4,.)
			forvalues t=1/4{
				if `t'==1{
					lincom ignite_0
				}
				else{
					lincom ignite_0_grpvar_`t'+ignite_0
				}
				
				local c=r(estimate)
				local ul=r(ub)
				local ll=r(lb)
				mat R[`t',1] = `c'
				mat R[`t',2] = `ul'
				mat R[`t',3] = `ll'
				mat R[`t',4] = `t'
				
			}
			
			
//Jail Effect by Quartiles			
			mat M=J(10,4,.)
			forvalues t=1/4{
				if `t'==1{
 					lincom stay
				}
				else{
					lincom stay_grpvar_`t'+stay
				}
				
				local c=r(estimate)
				local ul=r(ub)
				local ll=r(lb)
				mat M[`t',1] = `c'
				mat M[`t',2] = `ul'
				mat M[`t',3] = `ll'
				mat M[`t',4] = `t'
				
			}
			


			local xt "Quartiles Predicted Time in Jail"
			
			if "`v'"=="return_3m"{
				local name "Pr(Rebooked in 3 month after release)"
				local xlab xlab(-.6(.3).6)
			}
			if "`v'"=="num_maj_weekly"{
				local name "Number of Misconduct per Week"
				local xlab xlab(-1.5(.75)1.5)
			}

/*********************************************************************************
Panel (a) Effect of Months in IGNITE on Misconduct
Panel (b) Effect of Months in IGNITE on Recidivism
**********************************************************************************/				
			preserve
			
			svmat R
			keep if !mi(R1)
			keep R1-R4
			ren (R1 R2 R3 R4) (c ul ll t)
			
						
			local xub = 4+.2
			tw (rcap ul ll t, horizontal lc(black) mc(black)) ///
				(scatter t c, lc(black) mc(black)), ///
				`xlab' xline(0, lc(black)) ysc(r(.8 `xub')) ///
				xlab(-.6(.3).6) ylab(1 "(Low) 1" 2 "2" 3 "3" 4 "(High) 4")  ///
				legend(off) ytitle("`xt'") xtitle(" ") ///
				aspect(1)
				
			gr export "$figures/`v'_by_predtime_4_ignite.pdf", replace
			restore

/*********************************************************************************
Panel (c) Effect of Months in Jail on Misconduct
Panel (d) Effect of Months in Jail on Recidivism
**********************************************************************************/				
			preserve
			
			svmat M
			keep if !mi(M1)
			keep M1-M4
			ren (M1 M2 M3 M4) (c ul ll t)
			
			local xub = 4+.2
			tw (rcap ul ll t, horizontal lc(black) mc(black)) ///
				(scatter t c, lc(black) mc(black)), ///
				`xlab' xline(0, lc(black)) ysc(r(.8 `xub')) ///
				xlab(-1.5(.75)1.5) ylab(1 "(Low) 1" 2 "2" 3 "3" 4 "(High) 4")  ///
				legend(off) ytitle("`xt'") xtitle(" ") ///
				aspect(1)
				
			gr export "$figures/`v'_by_predtime_4.pdf", replace
			restore

			}

